﻿#pragma once

#include "common.h"
#include "shape.h"
#include "texture.h"
#include "ui/control_base.h"
#include "ui/dock_container.h"
//#include "ui/button.h"

namespace anip
{
	namespace ui
	{
		// Features:
		//  - Reflows when resized
		//  - Updates its parent's scrollable area when repositioned
		//  - Interacts with `AutoDockContainer`s when moving
		// Note: Use ui::MainWindow for top-level containers, though part of the logic
		// for top-level controls has been implemented in this class
		class Window : public AutoDockable<Scrollable<WithText<Container>>>
		{
		public:
			class ControlButton
			{
				friend class Window;

			private:
				bool 
					_hover = false, 
					_press = false;

			public:
				Texture2D* texture = nullptr;
				color backColor = Colors::white;
				std::function<void()> onClick;
				bool isHovered() const { return _hover; }

				ControlButton(const color& c) : backColor(c) {}
			};

		private:
			enum class member_with_flag(DragType)
			{
				None = 0, Move = 1, Top = 2, Bottom = 4, Left = 8, Right = 16
			};

			DragTypeFlags _dragType = DragType::None;

			vec2 
				_oldSize{}, 
				_undockSize{};

		protected:
			void processMouseMove(invec2 pos) override;
			void processMouseButton(MouseButton btn, ButtonAction action) override;
			void processFocus() override;
			void processDefocus() override;

			bool canBeginDrag(MouseButton btn, invec2 pos) override;
			void processDrag(invec2 start, invec2 pos) override;
			void processEndDrag() override;

			void processOnDocked() override;
			void processOnUndocked() override;

			wstring getTabTitle() const override { return getText(); }

			using Scrollable::setMargins;

		public:
			bool 
				allowResize = true,
				allowAutoDock = true;

			vec2 
				minimumSize = { 10, 10 };

			bcolor 
				textColor = r::Window_CaptionColor,
				headerColor = r::Window_HeaderColor,
				backColor = r::Window_BackColor;

			std::vector<ControlButton> 
				controlButtons;

			Window(Container* parent, invec2 size = {100, 100});

			ControlButton& addCloseButton();

			void paint() override;

			void setPosition(invec2 pos) override;
			void setSize(invec2 size) override;
			void setText(const wstring& f) override;
		};
	}
}